package com.ycsj.goldmedalnewconcept.teacher.activity;

import android.content.Intent;
import android.support.design.widget.Snackbar;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ycsj.common.base.BaseActivity;
import com.ycsj.common.base.BaseApplication;
import com.ycsj.common.utils.GsonUtil;
import com.ycsj.common.utils.SnackbarUtil;
import com.ycsj.common.utils.UIHelper;
import com.ycsj.common.utils.UiUtils;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.Constants;
import com.ycsj.goldmedalnewconcept.common.HttpAddress;
import com.ycsj.goldmedalnewconcept.common.http.HttpUtils;
import com.ycsj.goldmedalnewconcept.teacher.bean.KJResInfo;
import com.ycsj.goldmedalnewconcept.teacher.bean.TeamInfo;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

/**
 * 修改班级名称
 * Created by Administrator on 2018/6/27 0027.
 */

public class TeacherUpdateClassNameActivity extends BaseActivity implements View.OnClickListener {


    private LinearLayout llBaseMain;

    //返回
    private ImageView ivBack;

    //输出框
    private EditText etInput;

    //显示输入框的文字
    private TextView tvNum;

    //保存按钮
    private Button btSave;

    //班级信息
    private TeamInfo info;

    @Override
    protected int getContentLayout() {
        return R.layout.teacher_activity_update_class_name;
    }

    @Override
    protected void init() {


        initStateBar();

        //初始化视图
        initView();

        //设置监听
        initListener();

        //接受传值
        initIntent();
    }

    /**
     * 接受传值
     */
    private void initIntent() {

        info = (TeamInfo) getIntent().getSerializableExtra("classinfo");

    }


    /**
     * 初始化View
     */
    private void initView() {
        llBaseMain = findViewById(R.id.ll_main);
        etInput = findViewById(R.id.et_edit);
        btSave = findViewById(R.id.bt_commit);
        btSave.setOnClickListener(this);
        tvNum = findViewById(R.id.tv_num);
        ivBack = findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);
    }

    /**
     * 设置输入监听
     */
    private void initListener() {
        etInput.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String content = etInput.getText().toString().trim();
                if (!TextUtils.isEmpty(content)) {
                    if (content.length() > 7) {
                        tvNum.setText("8/8");
                        UiUtils.Toast("最多只能输入8字", false);
                    } else {
                        tvNum.setText(content.length() + "/8");
                    }
                } else {
                    tvNum.setText("0/8");
                }
            }
        });
    }

    /**
     * 保存后的操作
     */
    private void doSaveAction() {
        String content = etInput.getText().toString().trim();
        if (TextUtils.isEmpty(content)) {
            SnackbarUtil.ShortSnackbar(llBaseMain, "还未输入班级名称", Snackbar.LENGTH_SHORT).show();
            return;
        } else {
            //请求服务器上传修改的班级名称,上传成功后回显给学生管理界面
            uploadClassName(content);
        }
    }

    /**
     * 修改班级名称
     */
    private void uploadClassName(final String content) {
        Map<String, String> map = new HashMap<>();
        map.put("type", "8");
        map.put("id", info.getClassinfo().getId() + "");
        map.put("name", content);
        map.put("icon", info.getClassinfo().getIcon() + "");
        map.put("platform", "8");

        HttpUtils.post(HttpAddress.GETCLASS, map, this, new HttpUtils.CallBack() {

            @Override
            public void onError(Call call, Exception e, int id) {
                UiUtils.Toast(BaseApplication.ERROR, false);
            }

            @Override
            public void onResponse(String response, int id) {
                KJResInfo info = null;
                try {
                    info = GsonUtil.jsonToBean(response, KJResInfo.class);
                } catch (Exception e) {
                    Log.i("test", Constants.PARSEDATAERROR);
                }

                Log.i("test", "修改班级：" + response);
                if (null != info) {
                    if ("1".equals(info.getStatus())) {
                        UIHelper.showToast(TeacherUpdateClassNameActivity.this, "修改成功");
                        Intent intent = new Intent();
                        intent.putExtra("className", content);
                        setResult(RESULT_OK, intent);
                        finish();

                    } else {
                        //返回错误
                        UIHelper.showToast(TeacherUpdateClassNameActivity.this, "修改失败");

                    }
                } else {
                    Log.i("test", Constants.EMPTY);
                }
            }
        });
    }

    /**
     * 页面控件点击事件
     *
     * @param view
     */
    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.bt_commit) {
            //保存
            doSaveAction();
        } else if (id == R.id.iv_back) {
            //返回
            finish();
        }
    }
}
